package de.urw.app;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;

import android.util.Log;

import de.urw.core.renderActivity;

import modules.*;

public class ModuleLoader {
	private String path;
	//private 
	public ModuleLoader(String path){
		this.path = path;	
	}

	
	public HashMap<String, Class<?>> createIndex(){
		//Log.d("test", "start creating index");
		//Log.d("test", "path: " + this.path);
		
		//File folder = new File(this.path);
		//File folder = new File(renderActivity.assMan.open("modules"));
		
		/*Log.d("test", "folder: " + folder.getPath());
		Log.d("test", "abs: " + folder.getAbsolutePath());
		Log.d("test", "isDir: " + folder.isDirectory());
		Log.d("test", "isFile: " + folder.isFile());
		Log.d("test", "uri: " + folder.toURI());*/
		
		String[] list;
		
		/*String apkName = getPackageManager().getApplicationInfo(packageName, 0).sourceDir;
		PathClassLoader myClassLoader =
		        new dalvik.system.PathClassLoader(
		                    apkName,
		                ClassLoader.getSystemClassLoader());*/
		
		//SecureClassLoader cl = new SecureClassLoader();
		
		HashMap<String, Class<?>> classes = new HashMap<String, Class<?>>();
		
		try
		{
			list = renderActivity.assMan.list("modules");
			for(String s : list)
			{
				//Log.d("test", "start loop: " + s);
				Class<?> c;
				String className = s.replaceFirst("[.][^.]+$", "");
				//Log.d("test", "className: " + className);
				//c = loadClass("file:///android_asset/modules/", className);
				//beschiss!
				c = Class.forName("modules." + className);//ClassLoader.getSystemClassLoader().loadClass(className);//getSystemClassLoader().loadClass();
				//Log.d("test", "class loaded");
		        classes.put(className, c);
		        //Log.d("test", "class putted");
			}
		} 
		catch (Exception e1)
		{
			// TODO Auto-generated catch block
			Log.d("test", e1.toString());// + e1.getMessage());
			e1.printStackTrace();
		}
		
		
		
		/*File[] listOfFiles = folder.listFiles();
		
		Log.d("test", "created list of files");
		Log.d("test", "" + listOfFiles.length);*/
		
		
		
		
		
			/*for (File file : listOfFiles) {
				Log.d("test", "starting loop");
			    if (file.isFile()) {
			    	Log.d("test", "isFile == true");
			    	Class<?> c;
					try {
						String className = file.getName().replaceFirst("[.][^.]+$", "");
						Log.d("test", "className stuff");
						c = loadClass(this.path, className);
						Log.d("test", "class loaded");
				        classes.put(className, c);
				        Log.d("test", "class putted");
					} catch (Exception e) {
						// TODO Auto-generated catch block
						Log.d("test", "exception: " + e.getMessage());
						e.printStackTrace();
					}	    	
			    }
			}*/
			
			//Log.d("test", "index created");
			
		
		return classes;
	}
	
	
	private  Class<?> loadClass( String path, String className  ) throws Exception
	  {
	    URL url = new File( path ).toURI().toURL();

	    URLClassLoader cl = new URLClassLoader( new URL[]{ url } );

	    // modules is package name of the modules, very important
	    Class<?> c = cl.loadClass( "modules."+className );
		   
	    //evtl speicherleiche
	    //cl.close();
	    cl = null;
	    
	    return c;
	 }
	
		
}
